package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.MarketCouponUserExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author chengyi
 * @date 2022/10/13 14:31
 */
@Service
public class AdminCouponServiceImpl implements AdminCouponService {

    @Autowired
    MarketCouponMapper couponMapper;

    @Autowired
    MarketCouponUserMapper couponUserMapper;

    /**
     * 优惠券查询
     *
     * @param info
     * @param name
     * @param type
     * @param status
     * @return
     */
    @Override
    public CommonData<MarketCoupon> list(BasePageInfo info, String name, Short type, Short status) {
        //执行之前使用pageHelper开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());

        //创建example实例
        MarketCouponExample example = new MarketCouponExample();

        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketCouponExample.Criteria criteria = example.createCriteria();

        //判空
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null && !"".equals(type)) {
            criteria.andTypeEqualTo(type);
        }

        if (status != null && !"".equals(status)) {
            criteria.andStatusEqualTo(status);
        }


        List<MarketCoupon> list = couponMapper.selectByExample(example);
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);

    }

    /**
     * 添加优惠券
     *
     * @param marketCoupon
     * @return
     */
    @Override
    public MarketCoupon create(MarketCoupon marketCoupon) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date1 = null;
        try {
            date1 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketCoupon.setAddTime(date1);
        marketCoupon.setUpdateTime(date1);
        //插入优惠券信息
        couponMapper.insertSelective(marketCoupon);
        int id = marketCoupon.getId();
        MarketCoupon data = couponMapper.selectByPrimaryKey(id);
        return data;
    }

    /**
     * 编辑优惠券
     *
     * @param marketCoupon
     * @return
     */
    @Override
    public MarketCoupon update(MarketCoupon marketCoupon) {

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Date = formatter.format(date);
        java.util.Date date1 = null;
        try {
            date1 = formatter.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        marketCoupon.setUpdateTime(date1);
        //编辑优惠券信息
        couponMapper.updateByPrimaryKey(marketCoupon);
        int id = marketCoupon.getId();
        MarketCoupon data = couponMapper.selectByPrimaryKey(id);
        return data;
    }

    /**
     * 删除优惠券
     *
     * @param marketCoupon
     * @return
     */
    @Override
    public void delete(MarketCoupon marketCoupon) {

        //删除优惠券
        int id = marketCoupon.getId();
        couponMapper.deleteByPrimaryKey(id);
    }

    /**
     * 读取优惠券的详细信息
     *
     * @param id
     * @return
     */
    @Override
    public MarketCoupon read(Integer id) {
        MarketCoupon data = couponMapper.selectByPrimaryKey(id);
        return data;
    }

    /**
     * 查询指定用户的优惠券使用状态
     *
     * @param info
     * @param couponId
     * @param userId
     * @param status
     * @return
     */
    @Override
    public CommonData<MarketCouponUser> listuser(BasePageInfo info, Integer couponId, Integer userId, Short status) {

        //执行之前使用pageHelper开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());

        //创建example实例

        MarketCouponUserExample example = new MarketCouponUserExample();

//        MarketCouponExample.Criteria criteria = example.createCriteria();
        MarketCouponUserExample.Criteria criteria = example.createCriteria();

        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        //判空
        if (couponId != null && !"".equals(couponId)) {
            criteria.andCouponIdEqualTo(couponId);
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null && !"".equals(status)) {
            criteria.andStatusEqualTo(status);
        }

        List<MarketCouponUser> list = couponUserMapper.selectByExample(example);
        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(list);


        return CommonData.data(pageInfo);
    }
}
